Page({
  data: {
    name: "",
    phone: "",
    imageUrl: "",
    items: ["男", "女"],
    sex: undefined,
    discoverydate: new Date().toISOString().split("T")[0],
    imgdate: new Date().toISOString().split("T")[0],
    
  },
  bindPickerChange (data) {
    const { value } = data.detail;
    this.setData({
      sex: value
    })
  },
  bindNameInput: function (e) {
    this.setData({
      name: e.detail.value,
    });
  },
  bindPhoneInput: function (e) {
    this.setData({
      phone: e.detail.value,
    });
  },
  chooseImage: function () {
    var that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ["image"],
      sourceType: ["album", "camera"],
      maxDuration: 30,
      camera: "back",
      success(res) {
        that.setData({
          imageUrl: res.tempFiles[0].tempFilePath,
        });
        console.log(res.tempFiles.tempFilePath);
        console.log(res.tempFiles.size);
      },
    });
  },
  timeFormatSeconds: function(time) {
    let d = time ? new Date(time) : new Date();
    let year = d.getFullYear();
    let month = d.getMonth() + 1;
    let day = d.getDate();
    let hours = d.getHours();
    let min = d.getMinutes();
    let seconds = d.getSeconds();
    if (month < 10) month = '0' + month;
    if (day < 10) day = '0' + day;
    if (hours < 0) hours = '0' + hours;
    if (min < 10) min = '0' + min;
    if (seconds < 10) seconds = '0' + seconds;
 
    return (year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + seconds);
  },
  submitForm: function (e) {
    var formData = e.detail.value;
    formData.imageUrl = this.data.imageUrl;
    formData.sex = this.data.sex;
    formData.time = new Date().getTime();
    formData.formatData = this.timeFormatSeconds();
    console.log('-formData-', formData)
    var healthInfo = wx.getStorageSync("healthInfo") || {};
    if (healthInfo[formData.names]) {
      healthInfo[formData.names].push(formData)
    } else {
      healthInfo[formData.names] = [formData]
    }
    wx.setStorageSync("healthInfo", healthInfo);
    console.log('-formData-', formData)
    if (!(formData.names && formData.age && formData.sex !== undefined && formData.localtion && formData.imageUrl)) {
      wx.showToast({
        title: "请填写必填项",
        icon: "error",
        duration: 2000,
      });
      return;
    }
    wx.showToast({
      title: "提交成功",
      icon: "success",
      duration: 2000,
    });
    // 跳转infoDetail 页面
    wx.redirectTo({
      url: "../profileList/index?name=" + formData.name,
    });
  },
  bindDateChange: function (e) {
    this.setData({
      discoverydate: e.detail.value,
    });
  },
  bindImgDateChange: function (e) {
    this.setData({
      imgdate: e.detail.value,
    });
  },
});
